package com.ys.utils.base.j2se;

 
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 
 * @author SJ
 *
 */
public class SerialNumberUtil {
	/**自定义进制(0,1没有加入,容易与o,l混淆)*/
    private static final char[] r = new char[] { 'q', 'w', 'e', '8', 'a', 's', '2', 'd', 'z', 'x', '9', 'c', '7', 'p', 'o', '5', 'i', 'k', '3', 'm', 'j', 'u', 'f', 'r', '4', 'v','y', 'l'};
    /**自动补全组(不能与自定义进制有重复)*/
    private static final char[] b = new char[] { 't', 'n', '6', 'b', 'g', 'h' };
    /**进制长度*/
    private static final int l = r.length;
    /**序列最小长度*/
    private static final int s = 6;
 
    /**
     * 根据ID生成六位随机码
     * @param num ID
     * @return 随机码
     */
    public static String toSerialNumber(long num,int type) {
    	char[] buf = new char[32];
        int charPos = 32;
 
        while ((num / l) > 0) {
            buf[--charPos] = r[(int) (num % l)];
            num /= l;
        }
        buf[--charPos] = r[(int) (num % l)];
        String str = new String(buf, charPos, (32 - charPos));
        //不够长度的自动随机补全
        if (str.length() < s) {
            StringBuffer sb = new StringBuffer();
            Random rnd = new Random();
            for (int i = 0; i < s - str.length(); i++) {
                sb.append(b[rnd.nextInt(6)]);
            }
            str += sb.toString();
        }
        String suff="";
        if (type==1) {
			suff="a";
		}
        if (type==2) {
			suff="b";
		}
        if (type==3) {
			suff="c";
		}
        return suff+str;
    }
    
    public static void main(String[] args) throws Exception {
    	Map<String, String> map=new HashMap<String, String>();
		for (int i = 0; i < 1000000; i++) {
			String string=toSerialNumber(i,1);
			if (map.containsKey(string)) {
				throw new Exception("重复的CODE"+string);
			}
			map.put(string, string);
			System.out.println(string);
		}
	}
}